package com.cskaoyan.service.impl;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.MarketSystemExample;
import com.cskaoyan.bean.admin.vo.ExpressConfigDataVO;
import com.cskaoyan.bean.admin.vo.MarketConfigDataVO;
import com.cskaoyan.bean.admin.vo.OrderConfigDataVO;
import com.cskaoyan.bean.wx.vo.WxConfigDataVO;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.service.SystemService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author tangjian
 * @Date 2021/10/19 15:33
 * @description
 */
@Service
@Transactional
public class SystemServiceImpl implements SystemService {

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public MarketConfigDataVO queryMallConfig() {

        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(new MarketSystemExample());

        MarketConfigDataVO marketConfigDataVO = new MarketConfigDataVO();

        return  fillByList(marketConfigDataVO, marketSystems);
    }

    @Override
    public ExpressConfigDataVO queryExpressConfig() {

        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(new MarketSystemExample());

        ExpressConfigDataVO expressConfigDataVO = new ExpressConfigDataVO();

        return fillByList(expressConfigDataVO, marketSystems);
    }

    @Override
    public OrderConfigDataVO queryOrderConfig() {
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(new MarketSystemExample());

        OrderConfigDataVO orderConfigDataVO = new OrderConfigDataVO();

        return fillByList(orderConfigDataVO, marketSystems);
    }

    @Override
    public WxConfigDataVO queryWxConfig() {
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(new MarketSystemExample());

        WxConfigDataVO wxConfigDataVO = new WxConfigDataVO();

        return fillByList(wxConfigDataVO, marketSystems);
    }

    @SneakyThrows
    @Override
    public int update(MarketConfigDataVO marketConfigDataVO) {
        //已提取方法
        int i = updateMarketSystem(marketConfigDataVO);
        return i;
    }

    @Override
    public int update(ExpressConfigDataVO expressConfigDataVO) {

        int i = updateMarketSystem(expressConfigDataVO);

        return i;
    }

    @Override
    public int update(OrderConfigDataVO orderConfigDataVO) {

        int i = updateMarketSystem(orderConfigDataVO);

        return i;
    }

    @Override
    public int update(WxConfigDataVO wxConfigDataVO) {

        int i = updateMarketSystem(wxConfigDataVO);

        return i;
    }

    @SneakyThrows
    public<T> int updateMarketSystem(T t){
        int i = 0;

        MarketSystem marketSystem = new MarketSystem();
        Field[] declaredFields = t.getClass().getDeclaredFields();

        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);

            String key = declaredField.getName();
            String value = (String) declaredField.get(t);

            marketSystem.setKeyValue(value);
            marketSystem.setUpdateTime(new Date());

            MarketSystemExample marketSystemExample = new MarketSystemExample();
            marketSystemExample.createCriteria().andKeyNameEqualTo(key);

            i += marketSystemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
        }

        return i;
    }

    @SneakyThrows
    public static<T,E> T fillByList(T t, List<E> list){
        // TODO: 2021/10/19 可以把list 换成map匹配
        ArrayList<String> strings = new ArrayList<>();
        Field[] declaredFields = t.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            strings.add(declaredField.getName());
        }

        for (E e : list) {
            Field keyName = e.getClass().getDeclaredField("keyName");
            Field valueName = e.getClass().getDeclaredField("keyValue");
            keyName.setAccessible(true);
            valueName.setAccessible(true);

            if (strings.contains(keyName.get(e))){

                Field declaredField = t.getClass().getDeclaredField(((String) keyName.get(e)));
                declaredField.setAccessible(true);
                declaredField.set(t,valueName.get(e));
            }
        }

        return t;
    }
}
